/*
 * registerFile.c
 *
 *  Created on: Jan 30, 2013
 *      Author: Travis Drake, Ian Smith
 */

#include <stdlib.h>
#include "registerFile.h"
#include "random.h"

RegisterFilePtr RegisterFile_newRegisterFile()
{
	RegisterFilePtr rfptr = (RegisterFilePtr)malloc(sizeof(RegisterFileStr));
	int i;

	for (i = 0; i < REGISTERS; i++)
		rfptr->registers[i] = Register_newRegister();

	return rfptr;
}

void RegisterFile_free(RegisterFilePtr this)
{
	int i;
	for (i = 0; i < REGISTERS; i++)
		Register_deleteRegister(this->registers[i]);
}

RegisterStr RegisterFile_getRegisterWord(RegisterFilePtr this, int registerNumber)
{
	return Register_getRegister(this->registers[registerNumber]);
}

void RegisterFile_setRegisterWord(RegisterFilePtr this, int registerNumber, unsigned short value)
{
	Register_setRegister(this->registers[registerNumber], value);
	return;
}

//UNTESTED
Byte RegisterFile_getRegisterHOB(RegisterFilePtr this, int registerNumber)
{
	unsigned short reg = Register_getRegister(this->registers[registerNumber]);
	unsigned char byte_a = (unsigned char)((reg >> 8) & 0x00FF);
	return byte_a;
}

//UNTESTED
Byte RegisterFile_getRegisterLOB(RegisterFilePtr this, int registerNumber)
{
	unsigned short reg = Register_getRegister(this->registers[registerNumber]);
	unsigned char byte_b = (unsigned char)(reg & 0x00FF);
	return byte_b;
}

//UNTESTED
void RegisterFile_setRegisterHOB(RegisterFilePtr this, int registerNumber, Byte b)
{
	Register_setHOB(this->registers[registerNumber], b);
	return;
}

//UNTESTED
void RegisterFile_setRegisterLOB(RegisterFilePtr this, int registerNumber, Byte b)
{
	Register_setLOB(this->registers[registerNumber], b);
	return;
}




void RegisterFile_initialize(RegisterFilePtr this)
{
	int i;
	for (i = 0; i < REGISTERS; i++)
		Register_initialize(this->registers[i]);
}
